package com.cxl.price.top.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.cxl.price.bean.Brand;
import com.cxl.price.bean.Model;
import com.taobao.api.ApiException;
import com.taobao.api.domain.ItemProp;
import com.taobao.api.domain.PropValue;
import com.taobao.api.request.ItempropsGetRequest;
import com.taobao.api.response.ItempropsGetResponse;


public class TaoBaoApiService extends TopBaseService{
	
	//TaobaoClient client=new DefaultTaobaoClient(FinalStr.URL,FinalStr.APPKEY,FinalStr.SECRET);
	


	/**
	 * 得到类目属性下的 产品品牌
	 * @param cid
	 * @return
	 * @throws ApiException
	 */
	public List<Brand> getBrands(Long cid) throws ApiException
	{
		ItempropsGetRequest req=new ItempropsGetRequest();
		req.setFields("pid,name,must,multi,prop_values");
		req.setCid(cid);
		req.setPid(20000L);
		ItempropsGetResponse response = getTaobaoClient().execute(req);
		ItemProp Itemprops=response.getItemProps().get(0);
		List<Brand> list=new ArrayList<Brand>();
		Brand brand=null;
		int i=0;
		for(PropValue prop:Itemprops.getPropValues())
		{
			brand=new Brand();
			brand.setPid(20000L);
			brand.setVid(prop.getVid());
			brand.setName(prop.getName());
			brand.setSort(i);
			i++;
			list.add(brand);
		}
		return list;
	}
	
	
	/**
	 * 得到 产品品牌下的产品型号
	 * @param cid
	 * @return
	 * @throws ApiException
	 */
	public List<Model> getModels(Long cid,String pid_vid) throws ApiException
	{
		List<Model> list=new ArrayList<Model>();
		ItempropsGetRequest req=new ItempropsGetRequest();
		req.setFields("pid,name,must,multi,prop_values");
		req.setCid(cid);
		req.setChildPath(pid_vid);
		ItempropsGetResponse response;
		response = getTaobaoClient().execute(req);
		List<ItemProp> props=response.getItemProps();
		
		for(ItemProp prop:props)
		{
			Long pid=prop.getPid();
			List<PropValue> propValues=  prop.getPropValues();
			
			if(propValues==null)
				return null;
			
			Model model=null;
				for(PropValue propvalue:propValues)
				{
				model=new Model();
				String name=propvalue.getName().toLowerCase();
				Long vid=propvalue.getVid();
				model.setName(name);
				model.setPid(pid);
				model.setVid(vid);
				list.add(model);
				}
		}
		
		return list;
	}

	
//	public static void main(String[] args) throws ApiException {
//		TaoBaoApiService service=new TaoBaoApiService();
//		service.getBrands(110211L);
//	}
}
